https://leetcode.cn/problems/count-of-smaller-numbers-after-self/
class Solution {
public:
    int tmpNum[500010];
    int tmpIndex[500010];
    vector<int> index;
    vector<int>  ret;
    
    void megerSort(vector<int>& nums,int left,int right)
    {
        if(left>=right)
            return ;
        
        int mid=(left+right)>>1;
        //[left mid][mid+1,right]
        megerSort(nums,left,mid);
        megerSort(nums,mid+1,right);
        
        int cur1=left,cur2=mid+1,i=0;
        //排降序
        while(cur1<=mid&&cur2<=right)
        {
            if(nums[cur1]<=nums[cur2])
            {
                tmpNum[i]=nums[cur2];
                tmpIndex[i++]=index[cur2++];
            }   
            else
            {
                //计算ret[index]
                ret[index[cur1]]+=right-cur2+1;
                tmpNum[i]=nums[cur1];
        
                tmpIndex[i++]=index[cur1++];
            }
        }

        while(cur1<=mid)
        {
            tmpNum[i]=nums[cur1];
            tmpIndex[i++]=index[cur1++];
        }
        while(cur2<=right)
        {
            tmpNum[i]=nums[cur2];
            tmpIndex[i++]=index[cur2++];
        }

        for(int j=left;j<=right;j++)
        {
            nums[j]=tmpNum[j-left];
            index[j]=tmpIndex[j-left];
        }
    }


    
    vector<int> countSmaller(vector<int>& nums) {
        int n=nums.size();
        ret.resize(n);
        index.resize(n);
        for(int i=0;i<n;i++)
            index[i]=i;
        megerSort(nums,0,n-1);
        return ret;
    }
};